package com.koi.job.controller;

import cn.hutool.cron.CronUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.anno.KoiLog;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.job.service.job.SysJobService;
import com.koi.model.vo.system.SysJobVo;
import com.koi.pojo.system.SysJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ･ᴗ･
 * @description [SysJobController]
 */
@Slf4j
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysJobController {

    private final SysJobService sysJobService;

    /**
     * @description [多条件分页查询]
     * @author ･ᴗ･
     */
    @GetMapping("/sysJob/listPage")
    public IPage<SysJob> listPage(SysJobVo vo) {
        // 分页构造器
        Page<SysJob> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<SysJob> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(vo.getJobName()), SysJob::getJobName, vo.getJobName());
        wrapper.eq(StringUtils.isNotBlank(vo.getJobType()), SysJob::getJobType, vo.getJobType());
        wrapper.eq(StringUtils.isNotBlank(vo.getJobStatus()), SysJob::getJobStatus, vo.getJobStatus());
        // 进行分页查询
        page = sysJobService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysJob/list")
    public List<SysJob> list(SysJobVo vo) {
        LambdaQueryWrapper<SysJob> wrapper = new LambdaQueryWrapper<>();
        return sysJobService.list(wrapper);
    }

    /**
     * @description [根据ID查询数据]
     * @author ･ᴗ･
     */
    @GetMapping("/sysJob/getById/{id}")
    public SysJob getById(@PathVariable("id") Long id) {
        return sysJobService.getById(id);
    }

    /**
     * @description [删除任务]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除定时任务", operType = OperType.DELETE)
    @PostMapping("/sysJob/deleteById/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        // 1、停止定时任务
        CronUtil.remove(id + "");
        // 2、进行删除
        boolean remove = sysJobService.removeById(id);
        YUtil.isTrue(!remove, "删除任务失败，请重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除定时任务", operType = OperType.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/sysJob/batchDelete")
    public void batchDelete(@RequestBody List<Long> jobIds) {
        // 1、停止定时任务
        for (Long jobId : jobIds) {
            CronUtil.remove(jobId + "");
        }
        // 2、批量删除
        boolean batch = sysJobService.removeBatchByIds(jobIds);
        YUtil.isTrue(!batch, "删除任务失败，请重试");
    }

    /**
     * @description [任务调度状态修改]
     * @author ･ᴗ･
     */
    @KoiLog(title = "切换定时任务状态", operType = OperType.UPDATE)
    @PostMapping("/sysJob/updateStatus/{id}/{jobStatus}/{policyStatus}")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("jobStatus") String jobStatus, @PathVariable("policyStatus") String policyStatus) {
        sysJobService.updateStatus(id, jobStatus, policyStatus);
    }

    /**
     * @description [立即运行任务-执行一次]
     * @author ･ᴗ･
     */
    @KoiLog(title = "定时任务执行一次", operType = OperType.UPDATE)
    @GetMapping("/sysJob/runNow/{id}")
    public void runNow(@PathVariable Long id) {
        sysJobService.runNow(id);
    }

    /**
     * @description [添加定时任务]
     * @author ･ᴗ･
     */
    @KoiLog(title = "定时任务添加", operType = OperType.ADD)
    @PostMapping("/sysJob/add")
    public void addJob(@RequestBody SysJob job) {
        sysJobService.addJob(job);
    }

    /**
     * @description [修改定时任务]
     * @author ･ᴗ･
     */
    @KoiLog(title = "定时任务修改", operType = OperType.UPDATE)
    @PostMapping("/sysJob/update")
    public void updateJob(@RequestBody SysJob job) {
        sysJobService.updateJob(job);
    }

}
